//
//  CDVMBAlertView.m
//  testBluetooth
//
//  Created by LuoYue on 17/1/11.
//
//

#import <Foundation/Foundation.h>
#import "CDVMBAlertView.h"
#import <QuartzCore/QuartzCore.h>

static float localScale = 1;


@interface CDVMBAlertView ()
{
    BOOL _leftLeave;
}

@property (nonatomic, strong) UILabel *alertTitleLabel;
@property (nonatomic, strong) UILabel *alertContentLabel;
@property (nonatomic, strong) UIButton *leftBtn;
@property (nonatomic, strong) UIButton *rightBtn;
@property (nonatomic, strong) UIView *backImageView;
@property (nonatomic) float  localScale;

//注释图
@property (nonatomic, strong) UIView *mainView;

@end

@implementation CDVMBAlertView
#define kAlertWidth 275.0f * localScale
#define kAlertHeight 192.0f * localScale
#define kTitleYOffset 27.0f * localScale
#define kTitleHeight 22.5f * localScale
#define kDelta 30 * localScale

#define kContentOffset 50.0f * localScale
#define kBetweenLabelOffset 20.0f * localScale

#define kSingleButtonWidth 160.0f * localScale
#define kCoupleButtonWidth 100.0f * localScale
#define kButtonHeight 40.0f * localScale
#define kButtonBottomOffset 25.0f * localScale
#define kButtonLeftOffset   27.0f * localScale
#define kTitleFontSize 16.0f * localScale
#define kContentFontSize 12.0f * localScale
#define kButtonFontSize 14 * localScale
#define kXButtonSize 30 * localScale
+ (CGFloat)alertWidth
{
    return kAlertWidth;
}

+ (CGFloat)alertHeight
{
    return kAlertHeight;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)createView:(NSString *)title
      contentText:(NSString *)content
  leftButtonTitle:(NSString *)leftTitle
 rightButtonTitle:(NSString *)rigthTitle
{
    //容器背景设置成透明
    self.backgroundColor = [UIColor clearColor];
    self.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
    
    self.mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 265*localScale, 306*localScale)];
    self.mainView.center=CGPointMake(self.bounds.size.width / 2, self.bounds.size.height/2);
    self.mainView.autoresizingMask=self.autoresizingMask;
    [self addSubview:self.mainView];
    
    UIView* contentView =[[UIView alloc]initWithFrame:CGRectMake(0, 58*localScale, 265*localScale, 220*localScale)];
    contentView.backgroundColor=[UIColor whiteColor];
    contentView.layer.cornerRadius=10*localScale;
    contentView.layer.borderWidth=0;
    contentView.layer.borderColor=[[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3]CGColor];
    contentView.autoresizingMask=self.autoresizingMask;
    [self.mainView addSubview:contentView];
    
    UILabel* textView =[[UILabel alloc]initWithFrame:CGRectMake(22.5*localScale, 110.5*localScale, 220*localScale, 40*localScale)];
    textView.lineBreakMode=NSLineBreakByWordWrapping;
    textView.numberOfLines=3;
    textView.font=[UIFont systemFontOfSize:13.0f*localScale];
    textView.textColor=[UIColor colorWithRed:0x75/255.0 green:0x75/255.0 blue:0x75/255.0 alpha:1];
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@",content]];
    NSRange range = [content rangeOfString:title];
    [attributeString addAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:0 green:0xB9/255.0 blue:1 alpha:1]} range:range];
    textView.attributedText=attributeString;
    textView.autoresizingMask=self.autoresizingMask;
    [contentView addSubview:textView];
    
    UIButton* btnUpdate =[UIButton buttonWithType:UIButtonTypeCustom];
    leftTitle = leftTitle==nil?@"Update":leftTitle;
    btnUpdate.frame=CGRectMake(22.5*localScale, 165.5*localScale, 220*localScale, 40*localScale);
    btnUpdate.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
    btnUpdate.backgroundColor=[UIColor colorWithRed:0 green:0xB9/255.0 blue:1 alpha:1];
    btnUpdate.layer.cornerRadius=10;
    [btnUpdate setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnUpdate setTitle:leftTitle forState:UIControlStateNormal];
    [btnUpdate.titleLabel setFont:[UIFont systemFontOfSize:14.0f*localScale weight:0.3f]];
    btnUpdate.autoresizingMask=self.autoresizingMask;
    [btnUpdate addTarget:self action:@selector(leftBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:btnUpdate];
    
    UIButton* btnSkip =[UIButton buttonWithType:UIButtonTypeCustom];
    rigthTitle = rigthTitle==nil?@"Skip":rigthTitle;
    btnSkip.frame=CGRectMake(77.5*localScale, 285*localScale, 100*localScale, 20*localScale);
    btnSkip.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
    btnSkip.center=CGPointMake(btnUpdate.center.x, 295*localScale);
    btnSkip.layer.borderWidth=0;
    btnSkip.backgroundColor= [UIColor clearColor];
    [btnSkip setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnSkip setTitle:rigthTitle forState:UIControlStateNormal];
    [btnSkip.titleLabel setFont:[UIFont systemFontOfSize:13.0f*localScale]];
    btnSkip.autoresizingMask=self.autoresizingMask;
    [btnSkip addTarget:self action:@selector(rightBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.mainView addSubview:btnSkip];
    
    UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 265*localScale, 158*localScale)];
    imageView.image=[UIImage imageNamed:@"bg_ft.png"];
    imageView.autoresizingMask=self.autoresizingMask;
    [self.mainView addSubview:imageView];
}


- (id)initWithTitle:(NSString *)title
        contentText:(NSString *)content
    leftButtonTitle:(NSString *)leftTitle
   rightButtonTitle:(NSString *)rigthTitle
{
     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
         localScale = 1;
     } else {
         localScale = 1.5f;
     }
    if (self = [super init]) {
        [self createView:title contentText:content leftButtonTitle:leftTitle rightButtonTitle:rigthTitle];
    }
    return self;
}

- (void)leftBtnClicked:(id)sender
{
    _leftLeave = YES;
    [self dismissAlert];
    if (self.leftBlock) {
        self.leftBlock();
    }
}

- (void)rightBtnClicked:(id)sender
{
    _leftLeave = NO;
    [self dismissAlert];
    if (self.rightBlock) {
        self.rightBlock();
    }
}

- (void)show
{
    UIViewController *topVC = [self appRootViewController];
    self.frame=CGRectMake(0, 0, topVC.view.bounds.size.width, topVC.view.bounds.size.height);
    
    //self.frame = CGRectMake((CGRectGetWidth(topVC.view.bounds) - kAlertWidth - kDelta ) * 0.5, - kAlertHeight - 30, kAlertWidth + kDelta , kAlertHeight + kDelta);
//    self.frame = CGRectMake((CGRectGetWidth(topVC.view.bounds) - kAlertWidth - kDelta) * 0.5, (CGRectGetHeight(topVC.view.bounds) - kAlertHeight - kDelta) * 0.5, kAlertWidth + kDelta, kAlertHeight + kDelta);
    [topVC.view addSubview:self];
}

- (void)dismissAlert
{
    [self removeFromSuperview];
    if (self.dismissBlock) {
        self.dismissBlock();
    }
}

- (UIViewController *)appRootViewController
{
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *topVC = appRootVC;
    while (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
    }
    return topVC;
}


- (void)removeFromSuperview
{
    [self.backImageView removeFromSuperview];
    self.backImageView = nil;
    /*
    UIViewController *topVC = [self appRootViewController];
    CGRect afterFrame = CGRectMake((CGRectGetWidth(topVC.view.bounds) - kAlertWidth) * 0.5, CGRectGetHeight(topVC.view.bounds), kAlertWidth, kAlertHeight);
    
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.frame = afterFrame;
        if (_leftLeave) {
            self.transform = CGAffineTransformMakeRotation(-M_1_PI / 1.5);
        }else {
            self.transform = CGAffineTransformMakeRotation(M_1_PI / 1.5);
        }
    } completion:^(BOOL finished) {
        [super removeFromSuperview];
    }];
    */
    [super removeFromSuperview];
}

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    if (newSuperview == nil) {
        return;
    }
    UIViewController *topVC = [self appRootViewController];
    
    if (!self.backImageView) {
        self.backImageView = [[UIView alloc] initWithFrame:topVC.view.bounds];
        self.backImageView.backgroundColor = [UIColor blackColor];
        self.backImageView.alpha = 0.5f;
        self.backImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    }
    [topVC.view addSubview:self.backImageView];
    /*
    self.transform = CGAffineTransformMakeRotation(-M_1_PI / 2);
    CGRect afterFrame = CGRectMake((CGRectGetWidth(topVC.view.bounds) - kAlertWidth) * 0.5, (CGRectGetHeight(topVC.view.bounds) - kAlertHeight) * 0.5, kAlertWidth, kAlertHeight);
    [UIView animateWithDuration:3.35f delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.transform = CGAffineTransformMakeRotation(0);
        self.frame = afterFrame;
    } completion:^(BOOL finished) {
    }];
    
     */
    [super willMoveToSuperview:newSuperview];
}

@end

@implementation UIImage (colorful)

+ (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

@end
